package rpc

import (
	"context"
	"errors"
	pb "forum/idl/pb/post"
	"forum/pkg/e"
)

// PostCreate 创建帖子
func PostCreate(ctx context.Context, req *pb.PostRequest) (resp *pb.PostCommonResponse, err error) {
	resp, err = PostClient.PostCreate(ctx, req)
	if err != nil {
		return
	}

	if resp.Code != e.SUCCESS {
		err = errors.New(resp.Msg)
		return
	}
	return
}

// PostUpdate 更新帖子
func PostUpdate(ctx context.Context, req *pb.PostRequest) (resp *pb.PostCommonResponse, err error) {
	resp, err = PostClient.PostUpdate(ctx, req)
	if err != nil {
		return
	}

	if resp.Code != e.SUCCESS {
		err = errors.New(resp.Msg)
		return
	}
	return
}

// PostDelete 删除帖子
func PostDelete(ctx context.Context, req *pb.PostRequest) (resp *pb.PostCommonResponse, err error) {
	resp, err = PostClient.PostDelete(ctx, req)
	if err != nil {
		return
	}

	if resp.Code != e.SUCCESS {
		err = errors.New(resp.Msg)
		return
	}
	return
}

// PostShow 获取用户所有帖子
func PostShow(ctx context.Context, req *pb.PostRequest) (resp *pb.PostDetailResponse, err error) {
	resp, err = PostClient.PostShow(ctx, req)
	if err != nil {
		return
	}

	if resp.Code != e.SUCCESS {
		err = errors.New(e.GetMsg(int(resp.Code)))
		return
	}
	return
}

// PostShowOne 获取指定帖子
func PostShowOne(ctx context.Context, req *pb.PostRequest) (resp *pb.PostDetailResponse, err error) {
	resp, err = PostClient.PostShowOne(ctx, req)
	if err != nil {
		return
	}

	if resp.Code != e.SUCCESS {
		err = errors.New(e.GetMsg(int(resp.Code)))
		return
	}
	return
}
